using System;
using System.Collections.Generic;
using System.Text;

namespace HLSSignature
{
    /// <summary>
    /// Collection class for settings used by the SAFE Signature plug-in
    /// </summary>
    public class PluginConfig
    {
        /// <summary>
        /// Flag to determine whether or not the registry settings have been
        /// read during the current run of the program
        /// </summary>
        static bool m_RegistrySettingsRead = false;


        static string[] m_AcceptablePolicies = null;
        /// <summary>
        /// Collection of acceptable certificate policy OIDs that signature
        /// certificates must express during signing
        /// </summary>
        public static string[] AcceptablePolicies
        {
            get
            {
                if (!m_RegistrySettingsRead)
                {
                    ReadRegistrySettings();   
                }
                return m_AcceptablePolicies;
            }
        }

        
        static string m_TimestampUrl = null;
        /// <summary>
        /// The URL fo the timestamp server.  If this is null, then RFC3161 
        /// timestamps are not used, and revocation checking is performed in
        /// real time during all validation operations
        /// </summary>
        public static string TimestampUrl
        {
            get
            {
                if (!m_RegistrySettingsRead)
                {
                    ReadRegistrySettings();
                }
                return m_TimestampUrl;
            }
        }

        static long m_ClockSkew = 1000 * 60 * 5; //  5 minutes
        /// <summary>
        /// Maximum allowable time in milliseconds between the signature validation
        /// time and the time-validity of the OCSP resposnes.  This allows for 
        /// slight differences in the system clocks of the OCSP responder and timestamp
        /// responder
        /// </summary>
        public static long ClockSkew
        {
            get
            {
                if (!m_RegistrySettingsRead)
                {
                    ReadRegistrySettings();
                }
                return m_ClockSkew;
            }
        }


        private static Object registryReadLock = new Object();
        /// <summary>
        /// Reads the registry settings for the plug-in
        /// </summary>
        private static void ReadRegistrySettings()
        {
            lock (registryReadLock)
            {
                if (m_RegistrySettingsRead)
                {
                    // don't read the settings if another thread beat this
                    // one to the lock
                    return;
                }

                Microsoft.Win32.RegistryKey baseKey = 
                    Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software");
                baseKey = baseKey.CreateSubKey("Gemini Security Solutions");
                baseKey = baseKey.CreateSubKey("Office SAFE Plugin");
                
                object oClockSkew = baseKey.GetValue("ClockSkew");
                if( oClockSkew != null ) 
                {
                    long.TryParse((string)oClockSkew, out PluginConfig.m_ClockSkew);
                }
                object oTimestampUrl = baseKey.GetValue("TimestampUrl");
                if (oTimestampUrl != null)
                {
                    m_TimestampUrl = (string)oTimestampUrl;
                }
                object oPolicy = baseKey.GetValue("Policy");
                if( oPolicy != null )
                {
                    m_AcceptablePolicies = (string[])oPolicy;
                }
                if (m_AcceptablePolicies == null)
                {
                    m_AcceptablePolicies = new string[0];
                }
                m_RegistrySettingsRead = true;
            }
        }
    }
}
